//
//  LieShouZhiWeiView.m
//  NineDuanAPP
//
//  Created by 嗯，大葱 on 17/3/9.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "LieShouZhiWeiView.h"

#define kW  self.frame.size.width
#define kH  self.frame.size.height

@implementation LieShouZhiWeiView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configureBtnView];
//        注册传值通知,用来更换按钮的状态（最新或者是推荐）
     //   [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(passValueText:) name:@"传值LieShou" object:nil];
        
    }
    return self;
}
//实现方法
- (void)passValueText:(NSNotification *)notification {
    //获取userInfo
    NSDictionary *userInfo = notification.userInfo;
    //获取字符串 并且赋值
    [_oneBtn setTitle:userInfo[@"text"] forState:(UIControlStateNormal)] ;
    [_oneBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -_oneBtn.imageView.image.size.width, 0, _oneBtn.imageView.image.size.width)];
    [_oneBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _oneBtn.titleLabel.bounds.size.width, 0, -_oneBtn.titleLabel.bounds.size.width)];
}
- (void)configureBtnView {
    CGFloat x = 0;
    CGFloat y = 10;
    CGFloat wid = (kW - 3)/3;
    CGFloat hig = 30;
    NSArray *tiArray = @[@"全部",@"时间",@"状态"];
    for (int i = 0; i < tiArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(x, y, wid, hig);
        [btn setTitle:tiArray[i] forState:(UIControlStateNormal)];
      
        if (i !=0) {
            [btn setImage:[UIImage imageNamed:@"sanjiao211"] forState:(UIControlStateNormal)];
            [btn setImage:[UIImage imageNamed:@"sanjiao112"] forState:(UIControlStateSelected)];
            
            
            
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.image.size.width, 0, btn.imageView.image.size.width)];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, -btn.titleLabel.bounds.size.width)];
        }
        
        [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [btn setTitleColor:KBtnColor forState:(UIControlStateSelected)];
    
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
      /*
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(btn.frame), 20, 1, 15)];
        view.backgroundColor = kgrayColor;
       [self addSubview:view];
        */
        btn.tag = 101+i;
        if (btn.tag == 101) {
            _oneBtn = btn;
        }
        if (btn.tag == 102) {
            _twoBtn = btn;
        }
      
        if (btn.tag == 103) {
            _fourBtn = btn;
        }
        
        [btn addTarget:self action:@selector(handleBtnSelectAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        x += wid+1;
        [self addSubview:btn];
    }
    
    
}
- (void)handleBtnSelectAction:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectMainBtnAction:)]) {
        [self.delegate selectMainBtnAction:sender];
    }
}

@end
